package com.liu.my_blog_system.utils;

import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
 * @author liuchengjie
 */
@Data
public class Result {
    private Boolean success;
    private Integer code;
    private String message;
    private Map<String, Object> data = new HashMap<>();
    //构造器私有
    private Result(){}

    //通用返回成功
    public static Result ok() {
        Result result = new Result();
        result.setSuccess(ResultCodeEnum.SUCCESS.getSuccess());
        result.setCode(ResultCodeEnum.SUCCESS.getCode());
        result.setMessage(ResultCodeEnum.SUCCESS.getMessage());
        return result;
    }
    //通用返回失败，未知错误
    public static Result error(){
        Result result = new Result();
        result.setSuccess(ResultCodeEnum.UNKNOWN_ERROR.getSuccess());
        result.setCode(ResultCodeEnum.UNKNOWN_ERROR.getCode());
        result.setMessage(ResultCodeEnum.UNKNOWN_ERROR.getMessage());
        return result;
    }

    //设置结果，形参为结果枚举
    public static Result setResult(ResultCodeEnum resultCodeEnum) {
        Result result = new Result();
        result.setSuccess(resultCodeEnum.getSuccess());
        result.setCode(resultCodeEnum.getCode());
        result.setMessage(resultCodeEnum.getMessage());
        return result;
    }

    //通过链式编程返回类本身
    //自定义返回数据
    public Result data(Map<String, Object> map) {
        this.setData(map);
        return this;
    }

    //通用设置data
    public Result data(String key, Object value) {
        this.data.put(key, value);
        return this;
    }

    //自定义状态信息
    public Result code(Integer code) {
        this.setCode(code);
        return this;
    }

    //自定义返回结果
    public Result success(Boolean success) {
        this.setSuccess(success);
        return this;
    }

    //自定义返回信息
    public Result message(String message) {
        this.setMessage(message);
        return this;
    }
}
